﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Configuration;
using System.Windows.Forms;
using System.Drawing;
using System.Reflection;
using System.Threading.Tasks;

namespace RLGDashboard
{
    public partial class PostMortemConfig : Form
    {
        private PostMortemConfigOptions configurationOptions;
        private PostMortemConfigOptions oldConfig;
        private bool updateClicked = false;
        private bool screenInitialized = false;

        public PostMortemConfig(ref PostMortemConfigOptions configurationOptions)
        {
            updateClicked = false;

            this.oldConfig = new PostMortemConfigOptions();
            this.configurationOptions = configurationOptions;

            oldConfig.displayType1 = configurationOptions.displayType1;
            oldConfig.displayType2 = configurationOptions.displayType2;
            oldConfig.y1LineColors = configurationOptions.y1LineColors;
            oldConfig.y2LineColors = configurationOptions.y2LineColors;

            InitializeComponent();
            intializeScreen();
        }

        private void intializeScreen()
        {
            screenInitialized = false;

            switch (configurationOptions.displayType1)
            {
                case "Temperature": y1EGT.Checked = true; break;
                case "Pressure": y1PSI.Checked = true; break;
                case "RPM": y1RPM.Checked = true; break;
                case "AFR": y1AFR.Checked = true; break;
                case "Voltage": y1Voltage.Checked = true; break;
            } 
            switch (configurationOptions.displayType2)
            {
                case "Temperature": y2EGT.Checked = true; break;
                case "Pressure": y2PSI.Checked = true; break;
                case "RPM": y2RPM.Checked = true; break;
                case "AFR": y2AFR.Checked = true; break;
                case "Voltage": y2Voltage.Checked = true; break;
            }

            redrawScreen();
            screenInitialized = true;
        }

        private void redrawScreen()
        {
            string[] sensor1Names = null;
            string[] sensor1Colors = null;
            string[] sensor2Names = null;
            string[] sensor2Colors = null;

            switch (configurationOptions.displayType1)
            {
                case "Temperature": sensor1Names = configurationOptions.egtLabels;
                    sensor1Colors = configurationOptions.egtColors; break;
                case "Pressure": sensor1Names = configurationOptions.psiLabels;
                    sensor1Colors = configurationOptions.psiColors; break;
                case "RPM": sensor1Names = configurationOptions.rpmLabels;
                    sensor1Colors = configurationOptions.rpmColors; break;
                case "AFR": sensor1Names = configurationOptions.afrLabels;
                    sensor1Colors = configurationOptions.afrColors; break;
                case "Voltage": sensor1Names = configurationOptions.voltageLabels;
                    sensor1Colors = configurationOptions.voltageColors; break;
            }
            switch (configurationOptions.displayType2)
            {
                case "Temperature": sensor2Names = configurationOptions.egtLabels;
                    sensor2Colors = configurationOptions.egtColors; break;
                case "Pressure": sensor2Names = configurationOptions.psiLabels;
                    sensor2Colors = configurationOptions.psiColors; break;
                case "RPM": sensor2Names = configurationOptions.rpmLabels;
                    sensor2Colors = configurationOptions.rpmColors; break;
                case "AFR": sensor2Names = configurationOptions.afrLabels;
                    sensor2Colors = configurationOptions.afrColors; break;
                case "Voltage": sensor2Names = configurationOptions.voltageLabels;
                    sensor2Colors = configurationOptions.voltageColors; break;
            }

            initializeLabelsandCombos(configurationOptions.displayType1, y1EGT, y1Sensor1, y1Sensor2, y1Sensor3, y1Sensor4, y1Sensor1Cmbo, y1Sensor2Cmbo, y1Sensor3Cmbo, y1Sensor4Cmbo, sensor1Names, sensor1Colors);
            initializeLabelsandCombos(configurationOptions.displayType2, y2EGT, y2Sensor1, y2Sensor2, y2Sensor3, y2Sensor4, y2Sensor1Cmbo, y2Sensor2Cmbo, y2Sensor3Cmbo, y2Sensor4Cmbo, sensor2Names, sensor2Colors);
        }

        public void initializeLabelsandCombos(string displayType, RadioButton rdoBtn, Label s1, Label s2, Label s3, Label s4, ComboBox c1, ComboBox c2, ComboBox c3, ComboBox c4, string[] names, string[] colors)
        {
            s1.Visible = true;
            s2.Visible = true;
            s3.Visible = true;
            s4.Visible = true;
            c1.Visible = true;
            c2.Visible = true;
            c3.Visible = true;
            c4.Visible = true;

            s1.Text = names[0];
            c1.SelectedIndex = c1.FindStringExact(colors[0]);

            if (displayType == "Voltage" || displayType == "AFR")
            {
                s2.Visible = false;
                s3.Visible = false;

                c2.Visible = false;
                c3.Visible = false;

                s4.Visible = false;
                c4.Visible = false;
            }
            else
            {
                s2.Text = names[1];
                s3.Text = names[2];

                c2.SelectedIndex = c2.FindStringExact(colors[1]);
                c3.SelectedIndex = c3.FindStringExact(colors[2]);

                if (displayType == "Temperature")
                {
                    s4.Text = names[3];
                    c4.SelectedIndex = c4.FindStringExact(colors[3]);
                }
                else
                {
                    s4.Visible = false;
                    c4.Visible = false;
                }
            }
        }

        private void initializeComboBoxes(ComboBox combobox)
        {
            combobox.DropDownStyle = ComboBoxStyle.DropDownList;
            Type colorType = typeof(System.Drawing.Color);

            // We take only static property to avoid properties like Name, IsSystemColor ...
            PropertyInfo[] propInfos = colorType.GetProperties(BindingFlags.Static | BindingFlags.DeclaredOnly | BindingFlags.Public);
            foreach (PropertyInfo propInfo in propInfos)
            {
                combobox.Items.Add(propInfo.Name);
            }
        }

        private void enableRdoButtons()
        {
            y1EGT.Enabled = true;
            y1AFR.Enabled = true;
            y1RPM.Enabled = true;
            y1Voltage.Enabled = true;
            y1PSI.Enabled = true;

            y2EGT.Enabled = true;
            y2AFR.Enabled = true;
            y2RPM.Enabled = true;
            y2Voltage.Enabled = true;
            y2PSI.Enabled = true;

            if (y1EGT.Checked == true)
                y2EGT.Enabled = false;
            
            if (y1PSI.Checked == true)
                y2PSI.Enabled = false;

            if (y1AFR.Checked == true)
                y2AFR.Enabled = false;

            if (y1RPM.Checked == true)
                y2RPM.Enabled = false;

            if (y1Voltage.Checked == true)
                y2Voltage.Enabled = false;

            if (y2EGT.Checked == true)
                y1EGT.Enabled = false;

            if (y2PSI.Checked == true)
                y1PSI.Enabled = false;

            if (y2AFR.Checked == true)
                y1AFR.Enabled = false;

            if (y2RPM.Checked == true)
                y1RPM.Enabled = false;

            if (y2Voltage.Checked == true)
                y1Voltage.Enabled = false;
        }

        private void handleRdo(RadioButton rdo, int index, string preference)
        {
            if (rdo.Checked == true && screenInitialized == true)
            {
                enableRdoButtons();
                captureLineColors();
                configurationOptions.setPreferences(index, preference);
                redrawScreen();
            }
        }
        private void y1EGT_CheckedChanged(object sender, EventArgs e)
        {
            handleRdo(y1EGT, 0, "Temperature");
        }

        private void y1PSI_CheckedChanged(object sender, EventArgs e)
        {
            handleRdo(y1PSI, 0, "Pressure");
        }

        private void y1AFR_CheckedChanged(object sender, EventArgs e)
        {
            handleRdo(y1AFR, 0, "AFR");
        }

        private void y1Voltage_CheckedChanged(object sender, EventArgs e)
        {
            handleRdo(y1Voltage, 0, "Voltage");
        }

        private void y1RPM_CheckedChanged(object sender, EventArgs e)
        {
            handleRdo(y1RPM, 0, "RPM");
        }

        private void y2EGT_CheckedChanged(object sender, EventArgs e)
        {
            handleRdo(y2EGT, 1, "Temperature");
        }

        private void y2PSI_CheckedChanged(object sender, EventArgs e)
        {
            handleRdo(y2PSI, 1, "Pressure");
        }

        private void y2AFR_CheckedChanged(object sender, EventArgs e)
        {
            handleRdo(y2AFR, 1, "AFR");
        }

        private void y2Voltage_CheckedChanged(object sender, EventArgs e)
        {
            handleRdo(y2Voltage, 1, "Voltage");
        }

        private void y2RPM_CheckedChanged(object sender, EventArgs e)
        {
            handleRdo(y2RPM, 1, "RPM");
        }

        private void Apply_Click(object sender, EventArgs e)
        {
            updateClicked = true;
            captureLineColors();
            this.Close();
        }

        private void captureLineColors()
        {
            string[] line1Colors = null;
            string[] line2Colors = null;

            switch (configurationOptions.displayType1)
            {
                case "Temperature": line1Colors = configurationOptions.egtColors; break;
                case "Pressure": line1Colors = configurationOptions.psiColors; break;
                case "RPM": line1Colors = configurationOptions.rpmColors; break;
                case "AFR": line1Colors = configurationOptions.afrColors; break;
                case "Voltage": line1Colors = configurationOptions.voltageColors; break;
            }

            switch (configurationOptions.displayType2)
            {
                case "Temperature": line2Colors = configurationOptions.egtColors; break;
                case "Pressure": line2Colors = configurationOptions.psiColors; break;
                case "RPM": line2Colors = configurationOptions.rpmColors; break;
                case "AFR": line2Colors = configurationOptions.afrColors; break;
                case "Voltage": line2Colors = configurationOptions.voltageColors; break;
            }

            line1Colors[0] = y1Sensor1Cmbo.Text;

            if (configurationOptions.displayType1 != "AFR" && configurationOptions.displayType1 != "Voltage")
            {
                line1Colors[1] = y1Sensor2Cmbo.Text;
                line1Colors[2] = y1Sensor3Cmbo.Text;

                if (configurationOptions.displayType1 == "Temperature")
                {
                    line1Colors[3] = y1Sensor4Cmbo.Text;
                }
            }

            line2Colors[0] = y2Sensor1Cmbo.Text;


            if (configurationOptions.displayType2 != "AFR" && configurationOptions.displayType2 != "Voltage")
            {
                line2Colors[1] = y2Sensor2Cmbo.Text;
                line2Colors[2] = y2Sensor3Cmbo.Text;

                if (configurationOptions.displayType2 == "Temperature")
                {
                    line2Colors[3] = y2Sensor4Cmbo.Text;
                }
            }
        }
        private void PostMortemConfig_FormClosing(object sender, System.Windows.Forms.FormClosingEventArgs e)
        {
            if (updateClicked == false)
            {
                configurationOptions.displayType1 = oldConfig.displayType1;
                configurationOptions.displayType2 = oldConfig.displayType2;

                configurationOptions.y1LineColors = oldConfig.y1LineColors;
                configurationOptions.y2LineColors = oldConfig.y2LineColors;

                configurationOptions.sensor1Names = oldConfig.sensor1Names;
                configurationOptions.sensor2Names = oldConfig.sensor2Names;
                configurationOptions.egtColors = oldConfig.egtColors;
                configurationOptions.psiColors = oldConfig.psiColors;
                configurationOptions.afrColors = oldConfig.afrColors;
                configurationOptions.rpmColors = oldConfig.rpmColors;
                configurationOptions.voltageColors = oldConfig.voltageColors;
            }
        }

        public class PostMortemConfigOptions
        {
            public string displayType1 = "Temperature";
            public string displayType2 = "Pressure";

            public string[] sensor1Names;
            public string[] sensor2Names;

            public string[] y1LineColors;
            public string[] y2LineColors;

            public string[] egtLabels = { "EGT1", "EGT2", "EGT3", "EGT4" };
            public string[] psiLabels = { "PSI1", "PSI2", "PSI3" };
            public string[] afrLabels = { "AFR" };
            public string[] voltageLabels = { "Voltage" };
            public string[] rpmLabels = { "RPM1", "RPM2", "RPM3" };

            public string[] egtColors = { "Red", "Blue", "Green", "Orange" };
            public string[] psiColors = { "Red", "Blue", "Green" };
            public string[] rpmColors = { "Red", "Blue", "Green" };
            public string[] afrColors = { "Red" };
            public string[] voltageColors = { "Red" };

            public PostMortemConfigOptions()
            {
                //EGT Values
                egtLabels[0] = Properties.Settings.Default.egt1Name;
                egtLabels[1] = Properties.Settings.Default.egt2Name;
                egtLabels[2] = Properties.Settings.Default.egt3Name;
                egtLabels[3] = Properties.Settings.Default.egt4Name;

                egtColors[0] = Properties.Settings.Default.egtLine1Color;
                egtColors[1] = Properties.Settings.Default.egtLine2Color;
                egtColors[2] = Properties.Settings.Default.egtLine3Color;
                egtColors[3] = Properties.Settings.Default.egtLine4Color;

                //PSI Values
                psiLabels[0] = Properties.Settings.Default.psi1Name;
                psiLabels[1] = Properties.Settings.Default.psi2Name;
                psiLabels[2] = Properties.Settings.Default.psi3Name;

                psiColors[0] = Properties.Settings.Default.psiLine1Color;
                psiColors[1] = Properties.Settings.Default.psiLine2Color;
                psiColors[2] = Properties.Settings.Default.psiLine3Color;

                //EGT 1 Values
                rpmLabels[0] = Properties.Settings.Default.rpm1Name;
                rpmLabels[1] = Properties.Settings.Default.rpm2Name;
                rpmLabels[2] = Properties.Settings.Default.rpm3Name;

                rpmColors[0] = Properties.Settings.Default.rpmLine1Color;
                rpmColors[1] = Properties.Settings.Default.rpmLine2Color;
                rpmColors[2] = Properties.Settings.Default.rpmLine3Color;

                //Voltage values
                voltageLabels[0] = Properties.Settings.Default.voltageName;
                voltageColors[0] = Properties.Settings.Default.voltageLineColor;

                //EGT 1 Values
                afrLabels[0] = Properties.Settings.Default.afrName;
                afrColors[0] = Properties.Settings.Default.afrLineColor;

                sensor1Names = egtLabels;
                sensor2Names = psiLabels;
                y1LineColors = egtColors;
                y2LineColors = psiColors;
            }

            public void setPreferences(int lineIndex, string preference)
            {
                if (lineIndex == 0)
                {
                    setValues(ref displayType1, ref sensor1Names, ref y1LineColors, preference);
                }
                else
                {
                    setValues(ref displayType2, ref sensor2Names, ref y2LineColors, preference);
                }
            }

            private void setValues(ref string displayName, ref string[] sensorNames, ref String[] lineColors, string preference)
            {
                displayName = preference;
                
                switch (displayName)
                {
                    case "Temperature": sensorNames = egtLabels;
                        lineColors = egtColors; break;
                    case "Pressure": sensorNames = psiLabels;
                        lineColors = psiColors; break;
                    case "AFR": sensorNames = afrLabels;
                        lineColors = afrColors; break;
                    case "Voltage": sensorNames = voltageLabels;
                        lineColors = voltageColors; break;
                    case "RPM": sensorNames = rpmLabels;
                        lineColors = rpmColors; break;
                }
            }
        }
    }
}
